<?php

if (isset($_GET['clear-tweet-stack'])) {
  if (!isset($_GET['user_id']) || $_GET['user_id'] == '') {
    die('No identity in URL...Fuck you!!!');
  }
  include_once "../config.php";
  require_once "../util.php";
  header ('Content-type: text/html; charset=utf-8');
  connect_db();

  $user_id= $_GET['user_id'];
  $sql = "delete from tweet_stack where identity = '$user_id'";
  $ret = mysql_query($sql);
  if (!$ret) {
    echo "SQL:($sql) failed: " . mysql_error();
    return;
  }

  echo mysql_affected_rows() . "行已更新，请等待跳转。。。";
  echo sprintf('<META HTTP-EQUIV="refresh" CONTENT="3; URL=%s">', $_SERVER['HTTP_REFERER']);
  return;
}

// Update user 
if (isset($_POST['identity'])) {
  include_once "../config.php";
  require_once "../util.php";
  header ('Content-type: text/html; charset=utf-8');

  connect_db();

  $identity = $_POST['identity'];
  $user_id = $_POST['user_id'];
  $enabled = isset($_POST['enabled']) ? 1 : 0;
  if (!isset($_POST['indexed'])) {
    $index_date = -1;
  }


  $passwd = mysql_real_escape_string($_POST['password']);
  $categories = mysql_real_escape_string(str_replace('＃', '#', $_POST['categories']));
  $tags = mysql_real_escape_string(str_replace('＃', '#', $_POST['tags']));
  $poison_tags = mysql_real_escape_string(str_replace('＃', '#', $_POST['poison_tags']));
  $victim_keywords = mysql_real_escape_string(str_replace('＃', '#', $_POST['victim_keywords']));
  $next_action_time = mysql_real_escape_string($_POST['next_action_time']);

  if (isset($_POST['reset-action-time'])) {
    $next_action_time = -1;
  }


  $sql = sprintf('update local_user set passwd = "%s", categories = "%s", tags = "%s",
victim_keywords="%s", enabled=%d, poison_tags = "%s", next_action_time = %d
 where identity= "%s"',
                 $passwd, $categories, $tags, $victim_keywords, $enabled, $poison_tags, $next_action_time, $identity);
  $ret = mysql_query($sql);
  if (!$ret) {
    echo "SQL:($sql) failed: " . mysql_error();
    return;
  }

//  $sql = sprintf("insert into task(user_id, type, content) values('%s', 'update_user_info', 'passwd=%s&categories=%s&tags=%s&victim_keywords=%s&enabled=%d&poison_tags=%s&index_date=%d&next_action_time=%d')", $user_id, $passwd, $categories, $tags, $victim_keywords, $enabled, $poison_tags, $index_date, $next_action_time);
//  $ret = mysql_query($sql);
//  if (!$ret) {
//    echo "SQL:($sql) failed: " . mysql_error();
//    return;
//  }
  echo mysql_affected_rows() . "行已更新，请等待跳转。。。";

  echo sprintf('<META HTTP-EQUIV="refresh" CONTENT="3; URL=%s">', $_SERVER['HTTP_REFERER']);
  return;
}

if (!isset($_GET['identity']) || $_GET['identity'] == '') {
  echo "identity not found";
}
elseif (!isset($_GET['user_id']) || $_GET['user_id'] == '') {
    echo "user_id not found";
}

include_once "../config.php";
require_once "../util.php";

connect_db();

$identity = $_GET["identity"];
$user_id = $_GET["user_id"];
$sql = "select * from local_user where identity = '$identity'";
$ret = mysql_query($sql);
if (!$ret) {
  echo "'$sql' failed:<br/>";
  echo mysql_error();
  return;
}

if (mysql_num_rows($ret) != 1) {
  echo "'$sql' returned " . mysql_num_rows($ret) . " rows, WTF?";
  return;
}

$account = mysql_fetch_assoc($ret);

function fetch_tweet_stack($identity, $user_id) {
  $sql = "select count(*) as count from tweet_stack where user_id = '$user_id' and published = '0'";
  $ret = mysql_query($sql);
  if (!$ret) {
    echo "$sql failed:" . mysql_error();
    return;
  }
  $result = mysql_fetch_assoc($ret);
  echo "等待被发送的tweet条数: " . $result['count'];
  echo " -- <a href='edit_user.php?identity=$identity&&clear-tweet-stack'>删除全部</a>";
  $sql = "select title, href, created_at, id from tweet_stack, tweet_crawled where user_id = '$user_id' and id = tweet_id and published = 0 order by created_at desc limit 100";
  echo "<br />" .$sql;
  $ret = mysql_query($sql);
  if (!$ret) {
    echo "$sql failed:" . mysql_error();
    return;
  }
  while ($tweet = mysql_fetch_assoc($ret)) {
    echo "<br />" . sprintf("<a href='%s'>%s</a> -- %s", $tweet['href'], $tweet['title'], $tweet['created_at']) .
      sprintf(" -- <a tweet_id='%s'>X</a>", $tweet['id']);
  }

}

function fetch_victims($user_id) {
  $sql = "select count(*) as count from victim_crawled where user_id = '$user_id' and follow_date = -1";
  $ret = mysql_query($sql);
  if (!$ret) {
    echo "$sql failed:" . mysql_error();
    return;
  }
  $result = mysql_fetch_assoc($ret);
  echo "还可以粉" . $result['count'] . "人";
  $sql = "select victim from victim_crawled where user_id = '$user_id' and follow_date = -1 limit 50";
  echo "<br />" .$sql;
  $ret = mysql_query($sql);
  if (!$ret) {
    echo "$sql failed:" . mysql_error();
    return;
  }
  while ($victim = mysql_fetch_assoc($ret)) {
    echo "<br />" . sprintf("<a href='http://weibo.com/%s'>%s</a>", $victim['victim'], $victim['victim']);

  }

}

?>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <link rel="stylesheet" type="text/css" href="css/stats.css" />
    <link rel="stylesheet" type="text/css" href="../css/reset.css">
    <title><?php echo $identity?> | 微博账户管理</title>
  </head>
  <body>
    <div id="edit-user">
      <p id="email-p"><?php echo $identity?></p>
      <form id="edit-form" action="edit_user.php" method="post">
        <input type="hidden" name="identity" value="<?php echo $identity?>"></input>
        <input type="hidden" name="user_id" value="<?php echo $user_id?>"></input>
        <label>密码:</label> <input name="password" value="<?php echo $account['passwd']?>" type="text"/>
        <label>categories:</label> <input name="categories" value="<?php echo $account['categories']?>" type="text"/>
        <label>tags:</label> <input name="tags" value="<?php echo $account['tags']?>" type="text"/>
        <label>poison tags:</label> <input name="poison_tags" value="<?php echo $account['poison_tags']?>" type="text"/>
        <label>victim_keywords:</label> <input name="victim_keywords" value="<?php echo $account['victim_keywords']?>" type="text"/>
        <label>enabled:</label><input type="checkbox" name="enabled" <?php if ($account['enabled']) {echo 'checked=""';} ?> />
        <label>indexed:</label><input type="checkbox" name="indexed" <?php if ($account['index_date'] != -1) {echo 'checked="true"';} ?> />
             next action time: (<?= date('Y-m-d H:m:s', $account['next_action_time'])?>)
        <input type="hidden" name="next_action_time" value="<?= $account['next_action_time']?>" />
        <lable>reset next action time </lable><input type="checkbox" name="reset-action-time" />
       
        <input type="submit" value="更新" />
      </form>
    </div>

    <div id="user-stat">
      <?php fetch_tweet_stack($identity, $user_id); ?>
    </div>

    <div id="to-follow">
      <?php fetch_victims($user_id); ?>
    </div>
  </body>
</html>
